<?php

$oper = $_POST['oper'];

if (!isset($oper))
{
    die("Unknown command");
}

$admin = true;
require_once('auth_req.inc.php');

switch ($oper)
{

    case 'edit':
            try
            {
                $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $dbh->beginTransaction();
        
                $sth = $dbh->prepare('update groups set name=?, g_desc=? where g_id=?');
                $sth->execute(array($_POST['name'], $_POST['g_desc'], $_POST['id']));

                $dbh->commit();
            }
            catch (Exception $e)
            {
                $dbh->rollBack();
            }
        break;

    case 'add':
            try
            {
                $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $dbh->beginTransaction();

                $sth = $dbh->prepare('insert into groups (name, g_desc) values (?,?)');
                $sth->execute(array($_POST['name'], $_POST['g_desc']));

                $dbh->commit();
            }
            catch (Exception $e)
            {
                $dbh->rollBack();
            }

        break;

    case 'del':
            try
            {
                $dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
                $dbh->beginTransaction();

                $sth = $dbh->prepare('select count(*) as count from network_map where g_id=?');
                $sth->execute(array(intval($_POST['id'])));
                $alldb = $sth->fetchAll(PDO::FETCH_ASSOC); 

                if ($alldb[0]['count'] > 0) 
                {
                    $response['error'] = "Группа не может быть удалена в ней есть контроллеры !";
                    echo json_encode($response);
                }
                else
                {
                    $sth = $dbh->prepare('delete from groups where g_id=?');
                    $sth->execute(array(intval($_POST['id'])));
                }

                $dbh->commit();
            }
            catch (Exception $e)
            {
                $dbh->rollBack();
            }

        break;

}

?>